package br.ic.ufal.view;

import br.ufal.ic.core.*;
import javax.persistence.CascadeType;
import br.ufal.ic.dao.*;
import br.ufal.ic.dao.mock.MockFacadeDAO;
import br.ufal.ic.manager.RankingManager;
import br.ufal.ic.user.User;
import java.awt.EventQueue;
import java.util.Date;
import java.util.Random;
import javax.swing.JOptionPane;

/**
 *
 * @author Marcos Paulo
 */
public class Main {

    /**
     * Informações adicionais:
     * Versão utilizando um DAO mock(br.ufal.ic.dao.mock.UserMockDAO) que
     * não faz persistencia, para persitir dados sera necessario usar o
     * o br.ufal.ic.dao.hibernate.UserHibernateDAO  com as configurações
     * do adequadas.
     */
    public static void main(String args[]) throws Exception {

        FacadeDAO.setFacadeDAO(new MockFacadeDAO());

        initTestDB();

        EventQueue.invokeLater(new Runnable() {

            public void run() {

//                new TournamentGUI().setVisible(true);
                new MainGUI().setVisible(true);
                JOptionPane.showMessageDialog(null, "Usuarios cadastrados "
                        + "para teste: "
                        + "Nick name: marcos password: 123456, "
                        + "Nick name: paulo password: 123456 ");
            }
        });
    }

    /**
     * Cria base de dados para teste.
     */
    private static void initTestDB() {
        UserDAO userDAO = FacadeDAO.getInstance().getUserDAO();

        CubistInformation cubistInformation = new CubistInformation(
                "Marcos Paulo José de Melo Silva", new Address(
                "doutor zeferino rodrigues", "maceio", "Brasil"),
                new Date());

        cubistInformation.addTimeInMilliseconds(new Long(0 + 40 * 1000 + 1000));
        cubistInformation.addTimeInMilliseconds(new Long(0 + 50 * 1000 + 1000));

        userDAO.save(new User("marcos", "123456", cubistInformation));

        cubistInformation = new CubistInformation("Paulo de Melo Silva",
                new Address("doutor zeferino rodrigues", "maceio", "Brasil"),
                new Date());

        cubistInformation.addTimeInMilliseconds(new Long(0 + 35 * 1000 + 1000));
        cubistInformation.addTimeInMilliseconds(new Long(0 + 25 * 1000 + 1000));

        userDAO.save(new User("paulo", "123456", cubistInformation));

        DelegateDAO delegateDAO = FacadeDAO.getInstance().getDelegateDAO();

        TournamentDAO tournamentDAO = FacadeDAO.getInstance().getTournamentDAO();

        TournamentInformation tournamentInformation = new TournamentInformation(
                "mark-torneio", TournamentType.OFFICIAL, new Date());

        Delegate delegate = new Delegate(cubistInformation);

        delegateDAO.save(delegate);

        Tournament tournament = new Tournament(tournamentInformation,
                delegate);

        tournamentDAO.save(tournament);

        tournamentInformation = new TournamentInformation(
                "paulo-torneio", TournamentType.UNOFFICIAL, new Date());

        tournament = new Tournament(tournamentInformation,
                delegate);


        tournamentDAO.save(tournament);

        RankingManager rankingManager = RankingManager.getInstance();

        Ranking ranking = rankingManager.getGeneralRanking();
        Random random = new Random();
        for (User user : userDAO.findAll()) {

            Competitor competitor = new Competitor(user.getInformation(),
                    new Cube(CubeType.CUBE_3X3X3));
            Score score = new Score(competitor);
            score.setValue(random.nextInt(1000));
            ranking.addScore(score);
        }

    }
}
